package flc.ast.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.Utils;

import java.util.ArrayList;
import java.util.List;

import flc.ast.R;
import flc.ast.utils.CustomAttachPopup;
import flc.ast.view.BaseAvTrackView1;
import flc.ast.view.StkAudioTrackView1;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.common.basic.media.audio.IAudioPlayer;
import stark.common.basic.utils.MediaUtil;
import stark.common.basic.utils.RxUtil;

/**
 * @Author: XiaoDan
 * @Date: 2023/12/26
 * @Description:
 */
public class AudioMergerAdapter extends BaseAdapter {


    public List<String> mAudioPaths = new ArrayList<>();
    private List<ViewHolder> mHolderList = new ArrayList<>();
    private IAudioPlayer mAudioPlayer;
    public IAudioPlayer mCurAudioPlayer;

    public AudioMergerAdapter(IAudioPlayer audioPlayer) {
        mAudioPlayer = audioPlayer;
        mAudioPlayer.setListener(mAudioPlayListener);
    }

    public void addAudioPath(String audioPath) {
        mAudioPaths.add(audioPath);
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        if (mAudioPaths != null) {
            return mAudioPaths.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        if (mAudioPaths != null) {
            return mAudioPaths.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(Utils.getApp()).inflate(R.layout.item_rv_mix_style, null);
            holder = new ViewHolder();
            holder.initView(convertView);
            convertView.setTag(holder);
            mHolderList.add(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.bindData(mAudioPaths.get(position), position);
        return convertView;
    }

    public List<ViewHolder> getHolderList() {
        return mHolderList;
    }

    private ViewHolder mPlayHolder;

    private void handlePlayPause(ViewHolder holder, int position) {
        if (mPlayHolder == holder) {
            if (mCurAudioPlayer.isPlaying()) {
                mCurAudioPlayer.pause();
            }
            if (mAudioPlayer.isPlaying()) {
                mAudioPlayer.pause();
            } else {
                mAudioPlayer.resume();
            }
            return;
        }
        if (mPlayHolder != null) {
            mPlayHolder.mIvPlayPause.setImageResource(R.drawable.azantinz);
        }
        if (mCurAudioPlayer.isPlaying()) {
            mCurAudioPlayer.pause();
        }
        mPlayHolder = holder;
        mAudioPlayer.play(mAudioPaths.get(position));
    }

    private void handleDel(ViewHolder holder, int pos) {
        mHolderList.remove(holder);
        mAudioPaths.remove(pos);
        if (mPlayHolder == holder) {
            mAudioPlayer.stop();
            mPlayHolder = null;
        }
        if (listener != null) {
            listener.clickDelete(pos);
        }
        notifyDataSetChanged();
    }

    private void onStartTimeChanged(ViewHolder holder, long startTime) {
        if (mPlayHolder == holder) {
            mAudioPlayer.seekTo((int) startTime);
            mAudioPlayer.resume();
        } else {
            if (mPlayHolder != null) {
                mPlayHolder.mIvPlayPause.setImageResource(R.drawable.azantinz);
            }
            mPlayHolder = holder;
            mAudioPlayer.play(mAudioPaths.get(holder.mBindPos));
            holder.mTrackView.postDelayed(new Runnable() {
                @Override
                public void run() {
                    mAudioPlayer.seekTo((int) startTime);
                }
            }, 100);
        }
    }

    private void onEndTimeChanged(ViewHolder holder, long endTime) {
        onStartTimeChanged(holder, endTime);
    }

    private void onStartEndTimeChanged(ViewHolder holder, long startTime, long endTime) {
        onStartTimeChanged(holder, startTime);
    }

    private IAudioPlayer.IListener mAudioPlayListener = new IAudioPlayer.IListener() {
        @Override
        public void onUpdatePlayTime(int curPlayTime, int duration) {
            if (mPlayHolder != null) {
                mPlayHolder.mTrackView.setPlayTime(curPlayTime);
                mPlayHolder.mTrackView.setDuration(duration);
            }
        }

        @Override
        public void onPlayChange(boolean isPlaying) {
            if (mPlayHolder != null) {
                mPlayHolder.mIvPlayPause.setImageResource(isPlaying ? R.drawable.abofzhong : R.drawable.azantinz);
            }
        }
    };

    public class ViewHolder {

        public TextView mTvName;
        public ImageView mIvDel;
        public StkAudioTrackView1 mTrackView;
        public ImageView mIvPlayPause;
        public int mBindPos;
        public String mBindPath;

        public void initView(View parent) {
            if (parent == null) return;
            mTvName = parent.findViewById(R.id.tvName);
            mIvDel = parent.findViewById(R.id.ivClose);
            mTrackView = parent.findViewById(R.id.trackView);
            mIvPlayPause = parent.findViewById(R.id.ivPlay);
        }

        public void bindData(String audioPath, int position) {
            mBindPath = audioPath;
            mBindPos = position;
            String fileName = FileUtils.getFileName(audioPath);
            mTvName.setText(fileName);
            mIvDel.setOnClickListener(v -> {
                handleDel(this, position);
            });
            mIvPlayPause.setOnClickListener(v -> {
                handlePlayPause(this, position);
            });
            mTrackView.setListener(new BaseAvTrackView1.IListener() {
                @Override
                public void onStartTimeChanged(long startTime) {
                    AudioMergerAdapter.this.onStartTimeChanged(ViewHolder.this, startTime);
                }

                @Override
                public void onEndTimeChanged(long endTime) {
                    AudioMergerAdapter.this.onEndTimeChanged(ViewHolder.this, endTime);
                }

                @Override
                public void onStartEndTimeChanged(long startTime, long endTime) {
                    AudioMergerAdapter.this.onStartEndTimeChanged(ViewHolder.this, startTime, endTime);
                }
            });
            RxUtil.create(new RxUtil.Callback<Long>() {
                @Override
                public void doBackground(ObservableEmitter<Long> emitter) {
                    emitter.onNext(MediaUtil.getDuration(audioPath));
                }

                @Override
                public void accept(Long duration) {
                    mTrackView.setDuration(duration);
                }
            });

        }
    }

    private Ilistener listener;

    public void setListener(Ilistener listener) {
        this.listener = listener;
    }

    public interface Ilistener {

        void clickDelete(int position);
    }

}
